#! /bin/sh
#
# susan       This shell script takes care of starting and stopping
#	       the susan Director daemon
#
# chkconfig: 2345 90 9
# description: Backup Archiving REcovery Open Sourced.
#
# Copyright (C) 2011 Hechzh Property
# Modified to work on SuSE 1/31/2004 D. Scott Barninger <barninger at fairfieldcomputers.com>
# Added rc.status functions 3/13/2004 D. Scott Barninger <barninger at fairfieldcomputers.com>
# Added LSB init info 10/14/2004 D. Scott Barninger <barninger at fairfieldcomputers.com>
# Added $remote_fs, add susan specific daemon user & group Bruno Friedmann <bruno@ioda-net.ch>
# Added config check Philipp Storz <Philipp.Storz@skyatlas.net>

### BEGIN INIT INFO
# Provides:			 susan-dir
# Required-Start:	 $local_fs $remote_fs $network $time $syslog
# Should-Start:      postgresql mysql susan-sd susan-fd
# Required-Stop:	 $local_fs $remote_fs $network
# Should-Stop:       postgresql mysql susan-sd susan-fd
# Default-Start:	 3 5
# Default-Stop: 	 0 1 2 6
# Short-Description:	susan director
# Description:			Susan network backup system director daemon
### END INIT INFO

# source process status functions
# this gives us funtion rc_status -v to tell us if we succeed or fail
. /etc/rc.status

function checkcfg () {
   echo "Checking Configuration and Database connection ... "
   su -s /bin/sh @dir_user@ -c "@sbindir@/susan-dir -f -t"
   if [ $? -eq 0 ]; then
      return 0
   else
      return 1
   fi
}

RETVAL=0
case "$1" in
    start)
       checkcfg
       if [ $? -eq 0 ]; then
          echo -n "Starting the Susan Director: "
          /sbin/startproc -u @dir_user@ -g @dir_group@ @sbindir@/susan-dir $2
          rc_status -v && touch @subsysdir@/susan-dir
       else
          echo "Configuration check failed, please check log file for errors"
          rc_failed
       fi
       ;;
    stop)
       echo -n "Stopping the Director daemon: "
       /sbin/killproc @sbindir@/susan-dir
       rc_status -v && rm -f @subsysdir@/susan-dir
       ;;
    restart)
       $0 stop
       sleep 5
       $0 start
       ;;
    status)
       echo -n "Checking for Susan director "
       /sbin/checkproc @sbindir@/susan-dir
       rc_status -v
       ;;
    *)
       echo "Usage: $0 {start|stop|restart|status}"
       exit 1
       ;;
esac
rc_exit
